package com.blue.gateway.paycore;

import com.blue.gateway.common.Request;
import com.blue.gateway.common.Response;
import com.blue.gateway.constant.Constants;
import com.blue.gateway.dto.Result;
import com.blue.gateway.dto.ResultWrapper;
import com.blue.gateway.enums.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;

/**
 * @author xiaoyiming
 * @date 2020/5/4 12:05
 * @depiction
 */
@Slf4j
public class ChannelServiceDispatcher {

    @SuppressWarnings("all")
    public static <T extends Request, R extends Response> Result<R> request(T t) {
        if (t == null) {
            return ResultWrapper.failEnum(ResultCodeEnum.PARAM_ILLEGAL);
        }
        String beanName = t.getPayChannelEnum().getPayChannelValue() + t.getServiceChannelEnum().getValue() + Constants.BEAN_SERVICE_SUFFIX;
        IChannelService channelService = ChannelServiceFactory.getBean(beanName, AbstractBaseChannelService.class);
        if (channelService == null) {
            return ResultWrapper.failEnum(ResultCodeEnum.NOT_FIND_SERVICE);
        }
        return channelService.request(t);
    }
}
